package com.tvein.system.controller.FileLoader 
{
	//flash
	import flash.net.LocalConnection;
	//pureMVC
	import org.puremvc.as3.multicore.interfaces.ICommand;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.command.SimpleCommand;
	//custom
	import com.tvein.system.common.CommonConstants;
	import com.tvein.system.model.ServerProxy;
	import com.tvein.system.model.FilesProxy;
	
	public class SendFileNeed extends SimpleCommand implements ICommand
	{
		private var serverData:ServerProxy;
		private var filesData:FilesProxy;
		private var targetChannel:String;
		
		override public function execute(notification:INotification) : void
		{
			serverData = facade.retrieveProxy( ServerProxy.NAME ) as ServerProxy;
			filesData = facade.retrieveProxy( FilesProxy.NAME ) as FilesProxy;
			
			targetChannel = notification.getBody()[0];
			
			sendFile(notification.getBody()[1], notification.getBody()[2]);
		}
		
		private function sendFile(fileInfo:String, chunksID:int):void
		{
			var loadConnection:LocalConnection = new LocalConnection();
			
			for (var i:int = 0; i < filesData.filesArray.length; i++)
			{
				if ((filesData.filesArray[i].fileSrc + "|" + filesData.filesArray[i].fileVersion) == fileInfo)
				{
					loadConnection.send(targetChannel, "console", ["/fileloader -F", [fileInfo, chunksID, filesData.filesArray[i].fileChunks[chunksID]]]);
					
					break;
				}
			}
			
			loadConnection = null;
		}
	}
}